public class EditPlanEligibilityVF {

    private final Plan__c[] plan = new Plan__c[1];

    public String byRoleTitle {get; set;}
    public boolean roleDisabled {get; set;}
    public boolean titleDisabled {get; set;}
    
    private List<String> userTitleList = null;
    private Map<ID, UserRole> userRoleMap = null;
    
    private List<RoleWrapper> rolesList = new List<RoleWrapper>();
    private List<RoleWrapper> rolesDupList = new List<RoleWrapper>();
    private List<TitleWrapper> titlesList = new List<TitleWrapper>();
    private List<TitleWrapper> titlesDupList = new List<TitleWrapper>();

    public EditPlanEligibilityVF(ApexPages.StandardController myController)
    {
        this.plan[0] = (Plan__c)myController.getRecord();
        byRoleTitle = 'byTitle';
        roleDisabled = titleDisabled = false;
        setUserRoles();
        Plan__c[] plan = [select Type__c from Plan__c where id = :plan[0].id];
        
        if(plan.size() > 0 && plan[0].Type__c == 'Role')
            byRoleTitle = 'byRole';
            
        Plan_Role__c[] planRoleList = [select id, RoleId__c, RoleTitle__c, Description__c from Plan_Role__c where plan__c = :plan[0].id];
        if(planRoleList.size() > 0 && byRoleTitle == 'byTitle')
        {
            roleDisabled = !roleDisabled;
            for(Plan_Role__c pr: planRoleList)
            {
                TitleWrapper t = new TitleWrapper();
                t.selectedTitle = pr.RoleTitle__c;
                titlesList.add(t);  
            }
        }        
        else if(planRoleList.size() > 0 && byRoleTitle == 'byRole')
        {
            titleDisabled = !titleDisabled;
            for(Plan_Role__c pr: planRoleList)
            {
                RoleWrapper r = new RoleWrapper();
                r.selectedRole = pr.RoleId__c;
                rolesList.add(r);   
            }
        }        
        else
        {
            titlesList.add(new TitleWrapper());
            titlesList.add(new TitleWrapper());
            titlesList.add(new TitleWrapper());
            titlesList.add(new TitleWrapper());
            titlesList.add(new TitleWrapper());
        }
    }
    
/*  public void setUserTitles()
    {
        userTitleList = new List<String>();
        Map<String, string> tempTitleMap = new Map<String, string>(); 
        for(User[] usersList: [select Title from User where Title != '' and IsActive = true])
        {
            for(User u: usersList)
            {
                tempTitleMap.put(u.title, u.title);
            }
            if(tempTitleMap.size() == 1000)
                break;
        }
        if(tempTitleMap.size() > 0)
            userTitleList = tempTitleMap.values();
    }
    */
    
    public void setUserRoles()
    {
        userRoleMap = new Map<ID, UserRole>();
        for(UserRole[] uRoleList: [select Id, Name, Rollupdescription from UserRole order by Name])
        {
            for(UserRole ur: uRoleList)
                userRoleMap.put(ur.id, ur);
            if(userRoleMap.size() >= 1000)
                break;
        }
    }

    public PageReference addEligibility()
    {
        if(byRoleTitle == 'byTitle')
            titlesList.add(new TitleWrapper());
        else 
            rolesList.add(new RoleWrapper());
        return null;
    }    

    public PageReference saveEligibility()
    {
        for(Plan_Role__c[] pRole: [select id from Plan_Role__c where plan__c = :plan[0].id])
            delete pRole;
        
        Plan__c[] plan = [select Id, Type__c from Plan__c where id = :plan[0].id];
        if(byRoleTitle == 'byRole')
        {
            Map<String, String> tempMap = new Map<String, String>();
            for(RoleWrapper rw: rolesList)
            {
                if(rw.selectedRole != 'select')
                    tempMap.put(rw.selectedRole, rw.selectedRole);
            }
            if(plan.size() > 0)
            {
                plan[0].Type__c = 'Role';
                update plan;
            }
            if(tempMap.size() > 0)
            {
                List<String> selectedRoleList = tempMap.values();
                Plan_Role__c[] planRoleList = new Plan_Role__c[]{};
                Plan_Role__c planRole = null;
                UserRole ur = null;
                for(String roleId : selectedRoleList)
                {
                    if((ur = userRoleMap.get(roleId)) != null)
                    {
                        planRole = new Plan_Role__c(RoleID__c = roleId, plan__c = plan[0].Id, RoleTitle__c = ur.Name, Description__c = ur.RollupDescription);
                        planRoleList.add(planRole);
                    }
                }
                if(planRoleList.size() > 0)
                    insert planRoleList;
                    
            }           
                
        }
        else
        {
            Map<String, String> tempMap = new Map<String, String>();
            for(TitleWrapper tw: titlesList)
            {
                if(tw.selectedTitle != 'select')
                    tempMap.put(tw.selectedTitle, tw.selectedTitle);
            }
            if(plan.size() > 0)
            {
                plan[0].Type__c = 'Title';
                update plan;
            }
            
            if(tempMap.size() > 0)
            {
                List<String> selectedTitleList = tempMap.values();
                Plan_Role__c[] planRoleList = new Plan_Role__c[]{};
                Plan_Role__c planRole = null;
                for(String title : selectedTitleList)
                {
                    planRole = new Plan_Role__c(plan__c = plan[0].Id, RoleTitle__c = title);
                    planRoleList.add(planRole);
                }
                if(planRoleList.size() > 0)
                    insert planRoleList;
            }           
        }
        return null;
    }    

    public PageReference cancelEligibility()
    {
        return null;
    }    

    public PageReference removeEligibility()
    {
        if(byRoleTitle == 'byRole' && rolesList.size() > 0)
            rolesList.remove(rolesList.size() - 1);
        else if(titlesList.size() > 0)
            titlesList.remove(titlesList.size() - 1);
        return null;
    }    

    public PageReference checkSelection()
    {
        if(byRoleTitle == 'byTitle')
        {
            for(RoleWrapper ur: rolesList)
                rolesDupList.add(ur);
            rolesList.clear();
            if(titlesDupList.size() > 0)
            {
                for(TitleWrapper u: titlesDupList)
                    titlesList.add(u);
                titlesDupList.clear();
            }
            
        }
        else
        {
            for(TitleWrapper u: titlesList)
                titlesDupList.add(u);
            titlesList.clear();
            
            if(rolesDupList.size() == 0 && rolesList.size() == 0)
            {
                rolesList.add(new RoleWrapper());
                rolesList.add(new RoleWrapper());
                rolesList.add(new RoleWrapper());
                rolesList.add(new RoleWrapper());
                rolesList.add(new RoleWrapper());
            }
            
            if(rolesDupList.size() > 0)
            {
                for(RoleWrapper ur: rolesDupList)
                    rolesList.add(ur);
                rolesDupList.clear();
            }
        }
        return null;
    }    
    
    public class RoleWrapper 
    {
        public String selectedRole {get; set;}
        /*private List<UserRole> localRoleList = null;
        public RoleWrapper()
        {
            localRoleList = EditPlanEligibilityVF.userRoleList.deepClone();
        }
        */
        public List<SelectOption> getRoles() 
        {
            List<SelectOption> options = new List<SelectOption>();
            
            options.add(new SelectOption('select', 'Select'));
            for(UserRole[] uRoleList: [select Id, Name from UserRole order by Name])
            {
                for(UserRole ur: uRoleList)
                    options.add(new SelectOption(ur.Id, ur.Name));
                if(options.size() >= 1000)
                    break;
            }
            return options; 
        }
    }
    
    public List<RoleWrapper> getRoleRows() {
        return rolesList;
    }

    public class TitleWrapper 
    {
        public String selectedTitle {get; set;}
       /* private List<String> localTitleList = null;
        public TitleWrapper()
        {
            //localTitleList = EditPlanEligibilityVF.userTitleList.deepClone();
        }*/
        
        public List<SelectOption> getTitles() 
        {
            List<SelectOption> options = new List<SelectOption>();
            
            options.add(new SelectOption('select', 'Select'));
            Map<String, String> tempUserMap = new Map<String, String>(); 
            for(User[] usersList: [select Title from User where Title != ''])
            {
                for(User u: usersList)
                    tempUserMap.put(u.Title, u.Title);
                if(tempUserMap.size() >= 1000)
                    break;
            }
            List<String> titlesList = tempUserMap.values();
            
            for(String s: titlesList)
                options.add(new SelectOption(s, s));

            return options; 
        }
    }
    
    public List<TitleWrapper> getTitleRows() {
        return titlesList;
    }
 
}